#!/bin/env python
# -*- coding: utf-8 -*-
"""download hf repoid"""
import argparse
import pathlib
from huggingface_hub import hf_hub_download, snapshot_download


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='同步huggingface的模型到本地文件夹')
    parser.add_argument('-i', '--repoid',dest='repoid', type=str, help='id of huggingface repo')
    parser.add_argument('-f', '--file', dest='file', type=str, required=False, help='filename of huggingface repo if not given sync all')
    parser.add_argument('-c', '--cache-dir',dest='cache_dir', required=False, type=str, help='hf cache dir')
    parser.add_argument('-l', '--local-dir',dest='local_dir', required=False, type=str, default=".", help='local file store dir')
    args,argv = parser.parse_known_args()
    cache_dir: str = None
    if args.cache_dir:
        cache_dir = pathlib.Path(args.cache_dir).resolve()
    local_dir = pathlib.Path(args.local_dir).resolve()
    repo_id = args.repoid
    filename = args.file
    assert repo_id is not None, "repo id is required"
    print("> config:")
    print(f"repo id: {repo_id}")
    print(f"file: {filename}")
    print(f"cache dir: {cache_dir}")
    print(f"local dir: {local_dir}")
    if filename is None:
        print(f"download huggingface repo all")
        snapshot_download(repo_id=repo_id, cache_dir=cache_dir, local_dir=local_dir)
    else:
        print(f"download huggingface repo with file")
        hf_hub_download(repo_id=repo_id, filename=filename, cache_dir=cache_dir, local_dir=local_dir)
